<template>
  <el-row justify="center" style="margin-top: 20px; margin: 50px">
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="宠物名字">
        <el-input v-model="form.petName" />
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="form.age" />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.gender" placeholder="please select your sex">
          <el-option label="公" :value="0" />
          <el-option label="母" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="救助地点">
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item label="绝育状态">
        <el-select v-model="form.statement" placeholder="please write statement">
          <el-option label="未绝育":value="0" />
          <el-option label="已绝育":value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="接种状态">
        <el-select v-model="form.vaccStatement" placeholder="please write statement">
          <el-option label="未接种":value="0" />
          <el-option label="已接种":value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="身体状况">
        <el-input v-model="form.health" />
      </el-form-item>
      <el-form-item label="救助时间">
        <el-col :span="11">
          <el-date-picker
        v-model="form.aidTime"
        type="datetime"
        value-format="YYYY-MM-DD HH:mm:ss"
        placeholder="Select date and time"
      />
        </el-col>
      </el-form-item>
      <el-form-item label="其他描述">
        <el-input v-model="form.otherDescriptions" type="textarea" />
      </el-form-item>
      <el-row justify="center" style="margin-top: 20px; margin: 50px">
        <el-form-item label="新增救助照片">
      <el-upload
        class="avatar-uploader"
        action="http://localhost:7777/upload/uploadOneFile"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>
  </el-row>  
      <el-row justify="center" style="margin-top: 20px; margin: 50px">
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确认</el-button>
      </el-form-item>
    </el-row>
    </el-form>
  </el-row>
</template>

<script lang="ts" setup>
import { onMounted,ref } from 'vue';
import { assistance } from '../../http';
import router from  '../../router'
import type { UploadProps } from 'element-plus'
const imageUrl = ref('')
import { useRoute } from 'vue-router'
const route=useRoute()
onMounted(()=>{
  if(route.query.aidId!=undefined){
  form.value.aidId=route.query.aidId
  queryById()
  }
})

const queryById=async()=>{
  let result=await assistance.queryById(form.value.aidId)
  if(result.code==0){
    form.value=result.data
    imageUrl.value='http://localhost:7777/springboot/upload/'+form.value.photo
  }
}
const handleAvatarSuccess:UploadProps['onSuccess'] = (response,uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
  if(response.code==0){
    form.value.photo=response.data
    
  }
  console.log(form.value)
}
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
// do not use same name with ref
const form = ref({
  aidId: '',
  petName: '',
  age: '',
  gender: '',
  address: '',
  statement: '',
  vaccStatement: '',
  aidTime:'',
  health: '',
  otherStatements: '',
  photo:''
  
})

const onSubmit = async() => {
  let result=''
  if (form.value.aidId=='')
  {
    result=await assistance.add(form.value)
  }
  else
  {
    result=await assistance.edit(form.value)
  }
  if(result.code==0)
  {
    ElMessage.success("保存成功")
    router.push({path: '/pet2'})
    }
  }
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
